local skel = fk.CreateSkill {
  name = "efengqi__wusheng",
}

Fk:loadTranslationTable{
  ["efengqi__wusheng"] = "武圣",
  [":efengqi__wusheng"] = "你可以将一名角色的一张牌（除你外每角色每轮限一次）当【杀】使用或打出，然后你可以交给其一张红色牌并令其回复1点体力。",

  ["#efengqi__wusheng"] = "武圣：将一名角色的一张牌当【杀】使用或打出<br>（先选择此【杀】的目标，然后选择你的牌或选择转化牌的角色）",
  ["#efengqi__wusheng-choose"] = "武圣：选择一名角色的一张牌用以转化",
  ["#efengqi__wusheng-card"] = "武圣：选择 %dest 的一张牌",
  ["#efengqi__wusheng-give"] = "武圣：是否交给 %dest 一张<font color='red'>红色</font>牌并令其回复1点体力？",

  ["$efengqi__wusheng1"] = "刀锋所向，战无不克！",
  ["$efengqi__wusheng2"] = "逆贼，哪里走！",
}

skel:addEffect("viewas", {
  pattern = "slash",
  anim_type = "offensive",
  prompt = "#efengqi__wusheng",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    local card = Fk:cloneCard("slash")
    if #cards == 1 then
      card:addSubcards(cards)
    elseif table.every(Fk:currentRoom().alive_players, function (p)
      return p:isNude() or p == player or table.contains(player:getTableMark("efengqi__wusheng-round"), p.id)
      end) then return nil
    end
    card.skillName = skel.name
    return card
  end,
  before_use = function(self, player, use)
    use.extra_data = use.extra_data or {}
    if #use.card.subcards == 0 then
      local room = player.room
      local targets = table.filter(room:getOtherPlayers(player), function(p)
        return not table.contains(player:getTableMark("efengqi__wusheng-round"), p.id) and
        table.find(p:getCardIds("he"), function (id)
          local card = Fk:cloneCard("slash", Fk:getCardById(id).suit, Fk:getCardById(id).number)
          card.skillName = skel.name
          if use.tos then
            return not player:prohibitUse(card)
          else
            return not player:prohibitResponse(card)
          end
        end) ~= nil
      end)
      if #targets == 0 then return skel.name end
      local to = room:askToChoosePlayers(player, {
        min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false, targets = targets,
        prompt = "#efengqi__wusheng-choose"
      })[1]
      room:addTableMark(player, "efengqi__wusheng-round", to.id)
      local cards = table.filter(to:getCardIds("he"), function (id)
        local card = Fk:cloneCard("slash", Fk:getCardById(id).suit, Fk:getCardById(id).number)
        card.skillName = skel.name
        if use.tos then
          return not player:prohibitUse(card)
        else
          return not player:prohibitResponse(card)
        end
      end)
      use.extra_data.efengqi__wusheng_to = to.id
      if #cards == 1 then
        use.card:addSubcard(cards[1])
      else
        local card_data = {}
        local poxi_data = {
          to = to.id,
          skillName = skel.name,
          prompt = "#efengqi__wusheng-card::"..to.id,
        }
        local visible_data = {}
        local cards_data = table.filter(to:getCardIds("h"), function (id)
          return table.contains(cards, id)
        end)
        if #cards_data > 0 then
          table.insert(card_data, {"$Hand", cards_data})
          for _, id in ipairs(cards_data) do
            if not player:cardVisible(id) then
              visible_data[tostring(id)] = false
            end
          end
          if next(visible_data) == nil then visible_data = nil end
          poxi_data.visible_data = visible_data
        end
        cards_data = table.filter(to:getCardIds("e"), function (id)
          return table.contains(cards, id)
        end)
        if #cards_data > 0 then
          table.insert(card_data, {"$Equip", cards_data})
        end
        local ret = room:askToPoxi(player, {
          poxi_type = skel.name, cancelable = false, data = card_data, extra_data = poxi_data,
        })
        use.card:addSubcards(ret)
      end
    end
  end,
  after_use = function (self, player, use)
    local room = player.room
    if player.dead or player:isNude() then return end
    if use.extra_data and use.extra_data.efengqi__wusheng_to then
      local to = room:getPlayerById(use.extra_data.efengqi__wusheng_to)
      if to.dead or to == player then return end
      local give = room:askToCards(player, {
        min_num = 1, max_num = 1, include_equip = true, skill_name = skel.name, pattern = ".|.|heart,diamond",
        prompt = "#efengqi__wusheng-give::"..to.id
      })
      if #give > 0 then
        room:moveCardTo(give, Card.PlayerHand, to, fk.ReasonGive, skel.name, nil, false, player)
        if to:isWounded() and not to.dead then
          room:recover{
            who = to,
            num = 1,
            recoverBy = player,
            skillName = skel.name,
          }
        end
      end
    end
  end,
  enabled_at_play = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return not p:isNude() and not table.contains(player:getTableMark("efengqi__wusheng-round"), p.id)
    end)
  end,
  enabled_at_response = function (self, player, response)
    return table.find(Fk:currentRoom().alive_players, function (p)
      return not p:isNude() and not table.contains(player:getTableMark("efengqi__wusheng-round"), p.id)
    end)
  end,
})

Fk:addPoxiMethod{
  name = "efengqi__wusheng",
  prompt = function (data, extra_data)
    return extra_data.prompt
  end,
  card_filter = function (to_select, selected, data, extra_data)
    return #selected == 0
  end,
  feasible = function(selected, data)
    return #selected == 1
  end,
  default_choice = function(data)
    if not data then return {} end
    local cids = data[2][1]
    return cids
  end,
}

return skel
